<?php

namespace app\controller;

use app\Request;
use app\service\MenuService;
use think\facade\Db;
use think\facade\Log;
use think\Route;

class Menu extends Base
{

    private $menuService;
    public function __construct()
    {
        $this->menuService = new MenuService();
    }

    // 添加编辑
    public function updateOrAddMenu(Request $request){
//        $this->validate()
        //out($request->all());
        $req = $request->post();
        $id = $request->all('id');
        $name = $request->all('name');
        $route = $request->all('route');
        $parenId = $request->all('parentId');

        $data = [
            "name"=>$req['name'],
            "route"=>$req['route']
        ];

        $rule =  ['name'=>"require"];
        $this->validate($data,$rule);


        if (!empty($id)) {
            //更新
            $this->menuService->updateMenu($request);
            return $this->Success('修改成功');
        } else {
            $re = $this->menuService->addMenu($name,$route,$parenId);
            return $this->Success('添加成功');
            //插入
        }

    }

    // 删除
    public function delMenu(Request $request){

        $isDel = $request->all('isDel');
        $id = $request->all('id');
        if (empty($id)){
            return $this->Error('Id为空');
        }
        $pid = Db::table('menu')->where('id',$id)->field('parent_id')->select();
        if (!empty($parenId)){
            //
            return $this->Error('请先删除父类:id为'.$id);
        }
        $this->menuService->deleteMenu($request);
        return $this->Success('删除成功');

    }

    // 二级列表
    public function subordinate(Request $request){
        $data = Db::table('menu')->where('is_del','<>','1')->select();
        $menuTree= $this->menuService->buildMenuTree($data);
        out($menuTree);
        return $this->Success($menuTree);


    }

    public function test(Request $request){
        $adminId='6';
        if (7 ==$adminId){
            echo '2';
            $where=[];
           $data= Db::table('menu')->field('route')->select()->toArray();
        }else{
            echo '1';
            $data = Db::table('admin_user_role')
                ->alias('u')
                ->where('u.user_id',7)
                ->join('menu m','u.menu_id = m.id')
                ->field('m.route')
                ->select()->toArray();
        }
        $data = Db::table('admin_user_role')
            ->alias('u')
            ->where($where)
            ->join('menu m','u.menu_id = m.id')
            ->field('m.route')
            ->select()->toArray();
        out($data);
        //超管查询
        $where = [];
        $userId==6 && $where['u.user_id']=6;

        $arr =[];
        $URL= \think\facade\Request::url();
        //out($data);
        $arr = array_column($data, 'route');


        if (in_array($URL,$arr)){
            out(1);
        }
        else{
            out('2');
        }
        $lastSql = Db::getLastSql();
        echo $lastSql;

// 获取所有的SQL日志
//        $sqlLog = Log::getLog();
//        print_r($sqlLog);
        out($data);
    }
}